import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/lock_wifi/index.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";
import "package:ttlock_flutter/ttlock.dart";

class LockResult extends StatefulWidget {
  final Map lockInfo;

  const LockResult({super.key, required this.lockInfo});

  @override
  State<StatefulWidget> createState() => _LockResultState();
}

class _LockResultState extends State<LockResult> {
  void _unlock() {
    EasyLoading.show();
    TTLock.controlLock(widget.lockInfo["lockData"], TTControlAction.unlock,
      (lockTime, electricQuantity, uniqueId) {
        EasyLoading.showSuccess("开锁成功");
    }, (errorCode, errorMsg) {
        EasyLoading.showError("$errorCode, $errorMsg");
    });
  }

  void _getLockPower() {
    TTLock.getLockPower(widget.lockInfo["lockData"], (electricQuantity) {
      EasyLoading.showSuccess("The power: $electricQuantity ");
    }, (errorCode, errorMsg) {
      EasyLoading.showError("$errorCode, $errorMsg");
    });
  }

  void _addFingerprint() {
    int startDate = DateTime.now().millisecondsSinceEpoch;
    int endDate = startDate + 3600 * 24 * 30 * 1000;
    TTLock.addFingerprint(null, startDate, endDate, widget.lockInfo["lockData"],
        (currentCount, totalCount) {
      EasyLoading.showSuccess(
          "currentCount:$currentCount  totalCount:$totalCount");
    }, (fingerprintNumber) {
      //
      print("指纹密钥：》》》》$fingerprintNumber");
      EasyLoading.showSuccess("fingerprintNumber: $fingerprintNumber");
    }, (errorCode, errorMsg) {
      EasyLoading.showError("$errorCode, $errorMsg");
    });
  }

  @override
  void dispose() {
    // TTLock.resetLock(widget.lockData, () {}, (errorCode, errorMsg) {});
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "门锁添加",
      child: Center(
        child: Column(
          children: [
            SizedBox(height: 50,),
            const Text("说明：您可以在房间中关联此智能锁", style: TextStyle(fontSize: 16),),
            Padding(
              padding: EdgeInsets.only(top: 60, bottom: 30),
              child: GestureDetector(
                onTap: () {
                  _unlock();
                },
                child: Container(
                  width: 160,
                  height: 160,
                  padding: EdgeInsets.all(5),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(160),
                      color: Colors.white,
                      boxShadow: [BoxShadow(color: ThemeVars.colorPrimaryLight, spreadRadius: 5, blurRadius: 15, blurStyle: BlurStyle.outer)]
                  ),
                  child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(100),
                        color: Colors.white,
                        // border: Border.all(color: ThemeVars.colorTextTer)
                        boxShadow: [BoxShadow(color: ThemeVars.colorPrimaryLight, spreadRadius: 1, blurRadius: 20, blurStyle: BlurStyle.inner)]
                    ),
                    child: Icon(Icons.lock_outline, size: 24, color: ThemeVars.colorTextSec,),
                  ),
                ),
              ),
            ),
            Text("点击开锁检测", style: TextStyle(color: ThemeVars.colorTextSec),),
            SizedBox(height: 60,),
            SizedBox(
              width: MediaQuery.of(context).size.width - ThemeVars.paddingPage * 4,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(child: Button(
                      onPressed: () {
                        Stacker.pushNamedAndRemoveUntil(Keys.DEVICE_LOCK, ModalRoute.withName(Keys.MAIN));
                      },
                      child: const Text("完成")
                  ),),
                  if (widget.lockInfo["type"] == 2) ...[
                    SizedBox(width: 8,),
                    Expanded(child: Button(
                        onPressed: () {
                          Stacker.pushNamed(Keys.DEVICE_LOCK_WIFI, arguments: {"lockData": widget.lockInfo["lockData"], "lockId": widget.lockInfo["lockId"], "type": ConfigType.unset});
                        },
                        child: const Text("配置wifi")
                    ),),
                  ]
                ],
              )
            )
          ],
        ),
      ),
    );
  }
}
